package com.springboot.jeesite.common.distributedlock.demo;

import com.springboot.jeesite.common.queue.activemq.CompanyProducer;
import com.springboot.jeesite.common.queue.redismq.RedisSender;
import com.springboot.jeesite.common.utils.JedisUtils;
import org.apache.activemq.artemis.jms.client.ActiveMQQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;

import javax.jms.Destination;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/*@Api(tags = "分布式秒杀")
@RestController
@RequestMapping("/seckillDistributed")*/
public class SeckillDistributedController {
    private final static Logger LOGGER = LoggerFactory.getLogger(SeckillDistributedController.class);

    private static int corePoolSize = Runtime.getRuntime().availableProcessors();
    //调整队列数 拒绝服务
    private static ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, corePoolSize + 1, 10l, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(10000));

    private RedisSender redisSender;
    //@Autowired
    //private KafkaSender kafkaSender;
    @Autowired
    private CompanyProducer activeMQSender;
    @Autowired
    SeckillDistributedServiceImpl seckillDistributedService;

    /*@ApiOperation(value = "秒杀一(Rediss分布式锁)", nickname = "xxx")*/
    @PostMapping("/startRedisLock")
    public String startRedisLock(long seckillId) {
        //seckillService.deleteSeckill(seckillId);
        final long killId = seckillId;
        LOGGER.info("开始秒杀一");
        for (int i = 0; i < 1000; i++) {
            final long userId = i;
            Runnable task = new Runnable() {
                @Override
                public void run() {
                    String result = seckillDistributedService.startSeckilRedisLock(killId, userId);
                    LOGGER.info("用户:{}{}", userId, result);
                }
            };
            executor.execute(task);
        }
        try {
            Thread.sleep(15000);
            //Long  seckillCount = seckillService.getSeckillCount(seckillId);
            //LOGGER.info("一共秒杀出{}件商品",seckillCount);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "SUCCESS";
    }

    /*@ApiOperation(value = "秒杀二(zookeeper分布式锁)", nickname = "xxx")*/
    @PostMapping("/startZkLock")
    public String startZkLock(long seckillId) {
//		seckillService.deleteSeckill(seckillId);
        final long killId = seckillId;
        LOGGER.info("开始秒杀二");
        for (int i = 0; i < 10000; i++) {
            final long userId = i;
            Runnable task = new Runnable() {
                @Override
                public void run() {
                    String result = seckillDistributedService.startSeckilZksLock(killId, userId);
                    LOGGER.info("用户:{}{}", userId, result);
                }
            };
            executor.execute(task);
        }
        try {
            Thread.sleep(10000);
            //Long  seckillCount = seckillService.getSeckillCount(seckillId);
            //LOGGER.info("一共秒杀出{}件商品",seckillCount);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "SUCCESS";
    }

    /*@ApiOperation(value = "秒杀三(Redis分布式队列-订阅监听)", nickname = "xxx")*/
    @PostMapping("/startRedisQueue")
    public String startRedisQueue(long seckillId) {
        //redisUtil.cacheValue(seckillId+"", null);//秒杀结束
        //seckillService.deleteSeckill(seckillId);
        final long killId = seckillId;
        LOGGER.info("开始秒杀三");
        for (int i = 0; i < 1000; i++) {
            final long userId = i;
            Runnable task = new Runnable() {
                @Override
                public void run() {
                    if (JedisUtils.get(killId + "") == null) {
                        //思考如何返回给用户信息ws
                        redisSender.sendChannelMess("seckill", killId + ";" + userId);
                    } else {
                        //秒杀结束
                    }
                }
            };
            executor.execute(task);
        }
        try {
            Thread.sleep(10000);
			/*redisUtil.cacheValue(killId+"", null);
			Long  seckillCount = seckillService.getSeckillCount(seckillId);
			LOGGER.info("一共秒杀出{}件商品",seckillCount);*/
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "SUCCESS";
    }

    /*@ApiOperation(value = "秒杀四(Kafka分布式队列)", nickname = "xxx")*/
    @PostMapping("/startKafkaQueue")
    public String startKafkaQueue(long seckillId) {
        //seckillService.deleteSeckill(seckillId);
        final long killId = seckillId;
        LOGGER.info("开始秒杀四");
        for (int i = 0; i < 1000; i++) {
            final long userId = i;
            Runnable task = new Runnable() {
                @Override
                public void run() {
                    if (JedisUtils.get(killId + "") == null) {
                        //思考如何返回给用户信息ws
                        //kafkaSender.sendChannelMess("seckill", killId + ";" + userId);
                    } else {
                        //秒杀结束
                    }
                }
            };
            executor.execute(task);
        }
        try {
            Thread.sleep(10000);
			/*redisUtil.cacheValue(killId+"", null);
			Long  seckillCount = seckillService.getSeckillCount(seckillId);
			LOGGER.info("一共秒杀出{}件商品",seckillCount);*/
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "SUCCESS";
    }

    /*@ApiOperation(value = "秒杀五(ActiveMQ分布式队列)", nickname = "xxx")*/
    @PostMapping("/startActiveMQQueue")
    public String startActiveMQQueue(long seckillId) {
        //seckillService.deleteSeckill(seckillId);
        final long killId = seckillId;
        LOGGER.info("开始秒杀五");
        for (int i = 0; i < 1000; i++) {
            final long userId = i;
            Runnable task = new Runnable() {
                @Override
                public void run() {
                    if (JedisUtils.get(killId + "") == null) {
                        Destination destination = new ActiveMQQueue("seckill.queue");
                        //思考如何返回给用户信息ws
                        activeMQSender.sendMessage(killId + ";" + userId);
                    } else {
                        //秒杀结束
                    }
                }
            };
            executor.execute(task);
        }
        try {
            Thread.sleep(10000);
			/*redisUtil.cacheValue(killId+"", null);
			Long  seckillCount = seckillService.getSeckillCount(seckillId);
			LOGGER.info("一共秒杀出{}件商品",seckillCount);*/
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "SUCCESS";
    }
}
